#ifndef _MT5_LOGICBOARD_H_
#define _MT5_LOGICBOARD_H_

#include "mat_define.h"
#include "mat_gem.h"
#include "mat_pathfinding.h"
#include "mat_animation.h"

class MATScoring;
class MATSfx;

#include <string>
#include <vector>

struct MATGem;

class MATLogicBoard : public MATAniFinishEventReceiveable
{
public:

	

	
	

	static MATLogicBoard&		Instance();
	virtual void				OnAnimationFinish(MATAnimation::ANI_TYPE AnimationType);
	

	void						Clear();
	void						Tick(float dt);

	MATGem*						GetContentAt(int y,int x);
	void						SetContentAt(int y,int x, MATGem* Gem ); 
	bool						IsSelected(int y,int x)const { return (y == m_SelectedIndexY && x == m_SelectedIndexX); };
	
	MATGem*						MouseButtonDown(const math::Vec2& Pos, int Y, int X);
	void						MouseButtonReleased(const math::Vec2& Pos, int Y, int X, bool& ToShowNotValidMove);

	bool						IsFull()const;
	bool						IsAllBig()const;
	bool						IsAllTileClear()const;
	bool						IsEmpty() const;
	
	bool						CheckMatch5();
	void						ClearMatch();
	//MATScore&					GetScore()  { return m_Score; };
	const MATPathFinding*		GetPathFinding() const { return &m_PathFinding; };
	
	TILE_TYPE					GetTileAt(int Y, int X) { return m_Tiles[Y][X]; };
	void						ChangeTile(int y, int x, TILE_TYPE Type);

	void						LoadLevel(const std::string& File);
	void						Save(const std::string& File);
	bool						IsMatchedXY(int Y,int X);

	int							GetGemCount() const;
	void						ClearMarkedClearing();
	void						SetMarkedClearing(int Y, int X);

protected:

	void						CheckMatchXPlus();
	void						CheckMatchYPlus();
	void						CheckMatchXPlusYPlus();
	void						CheckMatchXPlusYMinus();
	void						CheckMatchXPlusYPlusLine(int StartY, int StartX);
	void						CheckMatchXPlusYMinusLine(int StartY, int StartX);	

	

	void						AddToSameList(const std::vector<int>& YList, const std::vector<int>& XList);

	MATGem*						m_Board[MAX_COUNT_Y][MAX_COUNT_X];
	TILE_TYPE					m_Tiles[MAX_COUNT_Y][MAX_COUNT_X];
	int							m_SelectedIndexX;
	int							m_SelectedIndexY;

	int							m_LastMoveX;
	int							m_LastMoveY;
	int							m_LastMoveToX;
	int							m_LastMoveToY;

	
	std::vector<int>			m_SameXs;
	std::vector<int>			m_SameYs;
	
	

	MATPathFinding				m_PathFinding;
	MATSfx*						m_pPart2dSelectedGem;
	math::Vec2					m_FirstMouseDownPos;
	

private:
	
	
	MATLogicBoard();
	void						ClearTiles();
	void						ClearTilesByMatch();
	

};

#endif
